<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建仓库':'编辑仓库'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="wareForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.id" hidden/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="所属区域" prop="areaPath">
          <el-cascader
            ref="area_cascader"
            v-model="form.areaPath"
            class="width-200"
            clearable
            placeholder="请选择地区..."
            :options="areaData"
            :props="areaProps"
            @change="handleAreaChange"
          />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" type="textarea" :rows="3" />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { areaTreeData, wareLoad, wareSave } from '@/api/mall/ware/ware'

export default {
  name: 'WareInfoFormDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      areaProps: {
        value: 'id',
        label: 'name',
        expandTrigger: 'hover',
        lazy: true,
        lazyLoad(node, resolve) {
          const { value } = node
          if (node.children !== undefined && node.children.length > 0) {
            resolve([])
            return
          }
          areaTreeData({
            from: 'vue',
            excludeRoot: 'true',
            parentId: value || 0
          }).then(response => {
            resolve(response)
          }).catch(e => {})
        }
      },
      areaData: [],
      form: {
        id: undefined,
        name: undefined,
        address: undefined,
        areaCode: undefined,
        areaPath: []
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '名称不能为空' }],
        areaPath: [{ required: true, trigger: 'blur', message: '所属区域不能为空' }],
        address: [{ required: true, trigger: 'blur', message: '地址不能为空' }]
      }
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
      })
    },
    loadForm(id) {
      wareLoad({ id: id }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        if (this.form.areaPath.length > 0) {
          this.initAreaData()
        }
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['mall:ware:edit'])
      }).catch(() => {
      })
    },
    async initAreaData() {
      await areaTreeData({
        from: 'vue',
        excludeRoot: 'true',
        parentId: 0,
        selected: this.form.areaPath.join(',')
      }).then(response => {
        this.areaData = response
      })
    },
    handleAreaChange() {
      let nodesInfo = this.$refs['area_cascader'].getCheckedNodes()
      if (nodesInfo.length > 0) {
        this.form.areaCode = nodesInfo[0].data.title
      } else {
        this.form.areaCode = undefined
      }
    },
    onSubmit() {
      this.$refs.wareForm.validate(valid => {
        if (valid) {
          wareSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.wareForm.resetFields()
      this.visible = false
    }
  }
}
</script>
